package controllers;

import java.util.Date;
import java.util.Map;

import models.user.Customer;
import models.user.CustomerLoginLog;
import play.cache.Cache;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.Before;
import play.mvc.Controller;

import com.fbpost.util.Constant;
import com.fbpost.util.NumberUtil;
import com.fbpost.util.StringUtil;

public class Application extends Controller {

	public static void index() {
		render();
	}

	/**
	 * 登录页改变language语言选项(仅登录页使用)
	 * 
	 * @param lang
	 */
	public static void loginLanguage(String language) {
		if (!StringUtil.isNull(language)) {
			Lang.set(language);
			response.setCookie("language", language, "365d");
		}
		login();
	}

	/*
	 * 显示登录界面
	 */
	public static void login() {
		String loginName = "";
		String loginPassword = "";
		String rememberMe = "";
		String language = "";

		if (request.cookies.get("loginName") != null) {
			loginName = request.cookies.get("loginName").value;
		}
		if (request.cookies.get("loginPassword") != null) {
			loginPassword = request.cookies.get("loginPassword").value;
		}
		if (request.cookies.get("rememberMe") != null) {
			rememberMe = request.cookies.get("rememberMe").value;
		}
		if (request.cookies.get("language") != null) {
			language = request.cookies.get("language").value;
		}
		if (StringUtil.isNull(language) || StringUtil.isEqual("null", language)) {
			// 先取是否已经有cookie 设置了 语言. 否则取请求头
			if (request.headers.get("Accept-Language") != null) {
				language = request.headers.get("Accept-Language").value();
			} else {
				language = "zh-CN";
			}
		}
		response.setCookie("language", language, "365d");
		Lang.set(language);
		session.clear();
		render(loginName, loginPassword, rememberMe, language);
	}

	/**
	 * 退出
	 */
	public static void logout() {
		session.clear();
		response.removeCookie("loginName");
		response.removeCookie("loginPassword");
		login();
	}

	/**
	 * 所有 Action权限检查
	 */
	@Before(unless = { "login", "loginLanguage", "logout", "authenticate", "test" })
	public static void secureCheck() {
		Secure secure = getControllerAnnotation(Secure.class);
		if (secure == null) {
			secure = getActionAnnotation(Secure.class);
		}
		if (secure != null) {
			if (secure.loginUser() && session.get("customerId") == null) {
				login();
			}
			if (secure.all()) {
				return;
			}
		} else {
			if (session.get("customerId") == null) {
				login();
			}
		}
	}

	/**
	 * 登录认证
	 * 
	 * @param loginName
	 * @param loginPassword
	 * @param language
	 * @param rememberMe
	 */
	public static void authenticate(String loginName, String loginPassword, String language, String rememberMe) {
		if (!StringUtil.isNull(rememberMe)) {
			response.setCookie("rememberMe", rememberMe, "31d");
		}
		if (!StringUtil.isNull(language)) {
			response.setCookie("language", language, "365d");
			Lang.set(language);
		}
		boolean isError = false;
		if (StringUtil.isNull(loginName)) {
			// 请输入账号
			validation.addError("loginName", Messages.get("pleaseEnterUserName"));
			isError = true;
		}
		if (StringUtil.isNull(loginPassword)) {
			// 请输入密码
			validation.addError("loginPassword", Messages.get("pleaseEnterPassword"));
		}
		if (isError) {
			render("@login", loginName, loginPassword, rememberMe, language);
		}
		// 登陆日志
		CustomerLoginLog log = new CustomerLoginLog();
		log.loginIp = request.remoteAddress;
		log.userBrowser = String.valueOf(request.headers.get("user-agent"));
		log.loginTime = new Date();
		log.loginName = loginName;
		log.loginPassword = loginPassword;
		log.language = language;
		log.save();
		// 这里不对登录名 密码等进行trim 动作 . 但注册强制不能包含空格.
		Customer customer = Customer.findCustomerByLoginName(loginName);
		if (customer == null) {
			// 帐号不存在
			validation.addError("loginName", Messages.get("accountNotExist"));
			render("@login", loginName, loginPassword, rememberMe, language);
		}

		// 真实密码
		String realPassword = customer.loginPassword;
		if (!StringUtil.isEqual(realPassword, loginPassword)) {
			// 密码不正确
			validation.addError("loginPassword", Messages.get("wrongPassword"));
			render("@login", loginName, loginPassword, rememberMe, language);
		}

		// 密码正确 开始检验客户状态

		if (NumberUtil.isEqualInt(customer.status, Constant.CUSTOMER_STATUS_UN_AUDITED)) {
			// 请等待审核
			validation.addError("loginName", Messages.get("pleaseWaitAudit"));
			render("@login", loginName, loginPassword, rememberMe, language);
		}
		if (NumberUtil.isEqualInt(customer.status, Constant.CUSTOMER_STATUS_NOT_APPROVED)) {
			// 审核未通过
			validation.addError("loginName", Messages.get("notApproved"));
			render("@login", loginName, loginPassword, rememberMe, language);
		}
		if (NumberUtil.isEqualInt(customer.status, Constant.CUSTOMER_STATUS_REFUSE)) {
			// 已拒绝登录
			validation.addError("loginName", Messages.get("denyLogon"));
			render("@login", loginName, loginPassword, rememberMe, language);
		}

		// 添加到cookie
		if (rememberMe != null && !"".equals(rememberMe)) {
			response.setCookie("loginName", loginName, "30d");
			response.setCookie("loginPassword", loginPassword, "30d");
		}
		// 登陆信息放入session 由于play cookie存储session方式，所以session不能存入太多东西
		Long customerId = customer.id;
		session.put("customerId", customerId);
		String url = session.get("url");
		if (url == null) {
			url = "/";
		}
		index();
	}
}